﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace COMMON
{
    public class Funcao
    {

        #region IsDate
        public static bool IsDate(string data)
        {
            DateTime dataValida;
            return DateTime.TryParse(data, out dataValida);
        }
        #endregion

        #region IsNumeric
        public static bool IsNumeric(string data)
        {
            int intValido;
            return int.TryParse(data, out intValido);
        }
        #endregion

        #region FormataData
        public static string FormataData(int segundos)
        {
            int hor = (int)(segundos / (60 * 60));
            int min = (int)((segundos - (hor * 60 * 60)) / 60);
            int seg = (int)(segundos - (hor * 60 * 60) - (min * 60));

            string dataFormatada = string.Format("{0}:{1}:{2}", RetornaStringMinima(hor.ToString(), 2), RetornaStringMinima(min.ToString(), 2), RetornaStringMinima(seg.ToString(), 2));

            return dataFormatada;
        }
        #endregion

        #region RetornaStringMinima
        public static string RetornaStringMinima(string valor, int qtd)
        {
            string stringFinal = valor;

            for (var a = 0; a < qtd; a++)
            {
                stringFinal = "0" + stringFinal;
            }

            stringFinal = stringFinal.Substring(stringFinal.Length - qtd);

            return stringFinal;
        }
        #endregion

        #region Alert
        public static void Alert(ClientScriptManager client, string mensagem)
        {
            client.RegisterClientScriptBlock(client.GetType(), "modal", "ShowMessage('Pop Promotora - Call Center', '" + mensagem + "');", true);
        }
        #endregion

        #region Alert
        public static void Alert(Page page, string mensagem)
        {
            AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(page, page.GetType(), "modal", "ShowMessage('Pop Promotora - Call Center', '" + mensagem + "');", true);
        }
        #endregion

        #region Encrypt
        private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        /// <summary>
        /// Encripta string
        /// </summary>
        /// <param name="stringToEncrypt">String para ser encriptada</param>
        /// <param name="sEncryptionKey">chave para encriptar</param>
        /// <returns>texto encriptado</returns>
        public static string Encrypt(string stringToEncrypt, string sEncryptionKey)
        {
            byte[] inputByteArray;

            try
            {
                var key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms,
                des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message.ToString());
            }

        }
        #endregion

        #region Decrypt
        /// <summary>
        /// Decripta string
        /// </summary>
        /// <param name="id"></param>
        /// <param name="encryptoKey"></param>
        /// <returns></returns>
        public static String Decrypt(string id, string encryptoKey)
        {
            byte[] inputByteArray = new byte[id.Length];

            try
            {
                var key = Encoding.UTF8.GetBytes(encryptoKey.Substring(0, 8));
                DESCryptoServiceProvider decrypt = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(id.Replace(' ', '+'));
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, decrypt.CreateDecryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                Encoding encoding = Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region DateTimeTryParseNull
        public static DateTime? DateTimeTryParseNull(string text)
        {
            DateTime date;
            if (DateTime.TryParse(text, out date))
            {
                return date;
            }
            return null;
        }
        #endregion

        #region ReturnListCheck
        public static IList<string> ReturnListCheck(CheckBoxList cbl)
        {
            var list = (from ListItem obj in cbl.Items where obj.Selected select obj.Value).ToList();
            return list;
        }
        #endregion

        #region ValidaLogin
        public static Boolean ValidaLogin(System.Web.HttpCookie oCookie)
        {
            var blnReturn = true;
            if (oCookie.Value == null)
                blnReturn = false;
            return blnReturn;
        }
        #endregion

    }
}